﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.orderLinqDAL;
using dhSystemBLL.orderData;
using baseSet;

namespace dhsystem.orderForm
{
    public partial class FrmcolorNumListAdd : Form
    {
        colorNumListBLL colorNum = new colorNumListBLL();
        colorNumListModel colorNumRow = new colorNumListModel();
        custListBLL cust = new custListBLL();
        BindingSource BindingSourcecolorNumAdd;
        string state;
        public FrmcolorNumListAdd(BindingSource _bindingSourcecolorNum, string _state)
        {
            InitializeComponent();
            BindingSourcecolorNumAdd = _bindingSourcecolorNum;
            state = _state;
        }
        //打开窗口时绑定数据
        private void FrmcolorNumListAdd_Load(object sender, EventArgs e)
        {
            bindingSourcecustList.DataSource = cust.getcustListAll();
            comboBoxcustNum.DataSource = bindingSourcecustList;
            comboBoxcustNum.DisplayMember = "custName";
            comboBoxcustNum.ValueMember = "custNum";

            if (state == "编辑")
            {
               
                comboBoxcustNum.DataBindings.Add("SelectedValue", BindingSourcecolorNumAdd, "custNum");
                textorderNo.DataBindings.Add("text", BindingSourcecolorNumAdd, "orderNo");
                textcolorName.DataBindings.Add("text", BindingSourcecolorNumAdd, "colorName");
                textcolorNuM.DataBindings.Add("text", BindingSourcecolorNumAdd, "colorNuM");
                textmark.DataBindings.Add("text", BindingSourcecolorNumAdd, "mark");

            }
        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (state == "新增")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否新增原材料资料！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        colorNumRow.orderNo = textorderNo.Text;
                        colorNumRow.custNum = comboBoxcustNum.SelectedValue.ToString();
                        colorNumRow.colorName = textcolorName.Text ;
                        colorNumRow.colorNuM = textcolorNuM.Text;
                        colorNumRow.mark = textmark.Text;
                        colorNumRow.updateDate = DateTime.Now;
                        colorNumRow.updateUser = GlobalInfo.UserName;
                        colorNum.colorNumListInsert(colorNumRow);
                        bindingSourcecolorNum.DataSource = colorNum.getcolorNumListAll();
                        MessageBox.Show("新增成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
            if (state == "编辑")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否修改原材料资料！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        colorNumRow.orderNo = textorderNo.Text;
                        colorNumRow.custNum = comboBoxcustNum.SelectedValue.ToString();
                        colorNumRow.colorName = textcolorName.Text;
                        colorNumRow.colorNuM = textcolorNuM.Text;
                        colorNumRow.mark = textmark.Text;
                        colorNumRow.updateDate = DateTime.Now;
                        colorNumRow.updateUser = GlobalInfo.UserName;
                        colorNum.colorNumListUpdate (colorNumRow);
                        bindingSourcecolorNum.DataSource = colorNum.getcolorNumListAll();
                        MessageBox.Show("修改成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }

        }
        //退出按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
